<?php
//no direct access
defined('_JEXEC') or die( 'Restricted access' );
//import the JController Class
jimport( 'joomla.application.component.model' );

class NvModelYoutube extends JModel {
	
	//var $tesURL = "http://gdata.youtube.com/feeds/api/videos?q=indonesia";
	
	function getVideoList($keyword,$amt){
		
		
		$keyEncoded = urlencode($keyword);
		
		$tesURL = "http://gdata.youtube.com/feeds/api/videos?q=".$keyEncoded."&max-results=".$amt;
		
	     
	  
		$options['rssUrl'] = $tesURL;
		
		//parse rss object
		$parser =  JFactory::getXMLParser('rss',$options);
		
				
		
		//initiate data object array
		$data = array();
		$index = 0;
		
		
		//get link,title and image url and store it to an object array
		foreach ($parser->get_items(0,$amt) as $item){
					
			$youtube = new stdClass();
			$youtube->title = $item->get_title();
			
			
			
		    $mediaGroup = $item->get_item_tags('http://search.yahoo.com/mrss/','group');
		    $thumbnail =  $mediaGroup[0]['child']['http://search.yahoo.com/mrss/']['thumbnail'];
		    $youtube->thumbnail = $thumbnail[0]['attribs']['']['url'];
		    
		    
			$content = $mediaGroup[0]['child']['http://search.yahoo.com/mrss/']['content'];
			$youtube->url = $content[0]['attribs']['']['url'];
			
			$description = $mediaGroup[0]['child']['http://search.yahoo.com/mrss/']['description'];
			$youtube->description = $description[0]['data'];
			
			$player = $mediaGroup[0]['child']['http://search.yahoo.com/mrss/']['player'];
			$youtube->player = $player[0]['attribs']['']['url'];
			
				    
			$video= explode("v=",$player[0]['attribs']['']['url']);
		    $youtube->videoid = $video[1];
		     		
			
			
			$youtube->link = $item->get_link();
			$youtube->authorname = $item->get_author()->get_name();
			$youtube->authorlink = $item->get_author()->get_link();
			
			$linkvideo = JRoute::_(JURI::base().'index.php?option=com_nv&view=youtube&layout=single
															&videoid='.$video[1].'
															&videotitle='.$youtube->title.'
															&videothumbnail='.$youtube->thumbnail.'
															&videourl='.$youtube->url.'
															&videodesc='.$youtube->description.'
															&videoplayer='.$youtube->player.'
															&videolink='.$youtube->link.'
															&videoauthorname=.'.$youtube->authorname.'
															&videoauthorlink='.$youtube->authorlink);
			$youtube->linkvideo = $video[1];
			
			
			$data[$index++] = $youtube;
						 			
		}
			
	 		
		return $data;
		
	}
	
	function getVideo($videoid){
		
		$reqUrl = "http://gdata.youtube.com/feeds/api/videos/".$videoid;
		
					
		$options['rssUrl'] = $reqUrl;
		echo $reqUrl;
		
		//parse rss object
		$parser =  JFactory::getXMLParser('Atom',$options);
		
				
		//initiate data object array
		$data = array();
		$index = 0;
		
		foreach ($parser->get_items(0,0) as $item){
			
			$video = new stdClass();
			
			$video->title = $item->get_title();
			
			$mediaGroup = $item->get_item_tags('http://search.yahoo.com/mrss/','group');
		    $thumbnail =  $mediaGroup[0]['child']['http://search.yahoo.com/mrss/']['thumbnail'];
		    $youtube->thumbnail = $thumbnail[0]['attribs']['']['url'];
		    
		    
			$content = $mediaGroup[0]['child']['http://search.yahoo.com/mrss/']['content'];
			$youtube->url = $content[0]['attribs']['']['url'];
			
			$description = $mediaGroup[0]['child']['http://search.yahoo.com/mrss/']['description'];
			$youtube->description = $description[0]['data'];
			
			$player = $mediaGroup[0]['child']['http://search.yahoo.com/mrss/']['player'];
			$youtube->player = $player[0]['attribs']['']['url'];
			
			$data[$index++] = $video;
		}
		
		return $data;
		
	}
}
?>